<?php

class albumComponents extends sfComponents
{
    public function connstr()
    {
        $server = "172.16.0.58";
        //$server="192.168.1.230";
        $username = 'sa';
        $password = '123456';
        $database = 'iEasy_new';
        $connstr = "Driver={SQL Server Native Client 10.0};Server=$server;Database=$database";
        $dbconn = odbc_connect($connstr, $username, $password, SQL_CUR_USE_ODBC);
        if (!$dbconn) {
            die('Could not connect: ');
        }
        else return $dbconn;
    }

    public function executeCommentlist()
    {
        if (empty($this->contentId))
            $this->contentId = "1076";
        if (empty($this->contentTypeId))
            $this->contentTypeId = "1003";
        if (empty($this->recordCount))
            $this->recordCount = "10";
        if (empty($this->currentPage))
            $this->currentPage = "1";
        $query = Doctrine_Query::create()->from("Comment c")
                ->where('content_id=' . $this->contentId)->andWhere('c.content_type_i=' . $this->contentTypeId);
        $query = $query->orderBy('c.create_time desc,c.modify_time desc');
        $this->pages = new sfDoctrinePager('Comment', $this->recordCount); //$this->currentPage
        $this->pages->setQuery($query); //$q   数据库查询语句
        $this->pages->setPage($this->currentPage); //
        $this->pages->init();
    }

    public function executeRecommend()
    {
        if ($this->catalogId == null) {
            $this->catalogId = 1000;
        }
        if ($this->recordCount == null) {
            $this->recordCount = 5;
        }
        $this->arrChinese = array();
        $this->arrKpop = array();
        $this->arrJpop = array();
        $this->arrWestern = array();
        $connection = Doctrine_Manager::connection();
        $query = 'SELECT snapshot,photo_group_id,status_id,title,RecommendContent.catalog_id as catalogid from RecommendContent, PhotoGroup
         where RecommendContent.content_id=PhotoGroup.photo_group_id and PhotoGroup.catalog_id>0 and status_id=1005 and RecommendContent.content_type_i=1003 order by RecommendContent.catalog_id,RecommendContent.order_i';
        $STH = $connection->execute($query);
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        while ($row = $STH->fetch()) {
            if ($row["catalogid"] == 1000) {
                $currentNews = array("photo_group_id" => $row["photo_group_id"], "snapshot" => $row["snapshot"], "title" => $row["title"], "statusid" => $row["status_id"]);
                $this->arrChinese[] = $currentNews;
            }
            if ($row["catalogid"] == 1001) {
                $currentNews = array("photo_group_id" => $row["photo_group_id"], "snapshot" => $row["snapshot"], "title" => $row["title"], "statusid" => $row["status_id"]);
                $this->arrKpop[] = $currentNews;
            }
            if ($row["catalogid"] == 1002) {
                $currentNews = array("photo_group_id" => $row["photo_group_id"], "snapshot" => $row["snapshot"], "title" => $row["title"], "statusid" => $row["status_id"]);
                $this->arrJpop[] = $currentNews;
            }
            if ($row["catalogid"] == 1003) {
                $currentNews = array("photo_group_id" => $row["photo_group_id"], "snapshot" => $row["snapshot"], "title" => $row["title"], "statusid" => $row["status_id"]);
                $this->arrWestern[] = $currentNews;
            }
        }
    }

    public function executeRelated(sfWebRequest $request)
    {
        if ($this->contentTypeId == null) {
            $this->contentTypeId = 1003;
        }
        if ($this->recordCount == null) {
            $this->recordCount = 9;
        }
        if ($this->contentId == null) {
            $this->contentId = 1001;
        }
        $this->photoArray = array();
        if ($this->layout == null) {
            $this->layout = 1;
        }
        $this->moreLinkDisplay = $request->getParameter("moreLinkDisplay", "none");
        $i = 0;
        if ($this->keyName == null)
            $this->keyName = "";
        $connection = Doctrine_Manager::connection();
        /*$sqlStr = "select top " .$this->recordCount." photo_group_id,title as title,create_time as currentTime, snapshot as snapshot from [PhotoGroup]
         where photo_group_id in ( select r_content_id from RelatedContent where r_content_type_i=1003 and content_type_i="
                .$this->contentTypeId." and (status_id=1005 or is_audit_b=0) and content_id=".$this->contentId;
        $sqlStr = $sqlStr.") order by create_time desc";*/ //and is_recommend_b=1
        $sqlStr = "select  photo_group_id,title as title,create_time as currentTime, snapshot as snapshot from PhotoGroup
         where status_id=1005 and catalog_id>0 and tag like '%" . $this->keyName . "%'  order by create_time desc limit " . $this->recordCount;
        if (1 == $this->layout) {
            $sqlStr = "select  photo_group_id,title as title,create_time as currentTime, snapshot as snapshot from PhotoGroup
         where photo_group_id in ( select r_content_id from RelatedContent where r_content_type_i=1003 and content_type_i="
                      . $this->contentTypeId . " and (status_id=1005 or is_audit_b=0) and content_id=" . $this->contentId;
            $sqlStr = $sqlStr . ") order by create_time desc limit " . $this->recordCount;
        }

        $STH = $connection->execute($sqlStr);
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        while ($row_array = $STH->fetch()) {
            $currentNews = array("photo_group_id" => $row_array["photo_group_id"], "title" => $row_array["title"], "currentTime" => $row_array["currentTime"], "snapshot" => $row_array["snapshot"]);
            $this->photoArray[] = $currentNews;
            $i++;
        }
    }

    public function executeRecommend2(sfWebRequest $request)
    {
        if ($this->catalogId == null) {
            $this->catalogId = 1000;
        }
        if ($this->recordCount == null) {
            $this->recordCount = 9;
        }
        $this->photoArray = array();
        $this->moreLinkDisplay = $request->getParameter("moreLinkDisplay", "none");
        $sqlStr = "select p.photo_group_id photo_group_id,p.title title,p.publish_time as currentTime,p.snapshot snapshot
        from PhotoGroup p where p.status_id=1005 and p.catalog_id>0 order by p.is_recommend_b desc ,p.is_on_top_b desc,p.publish_time desc limit " . $this->recordCount;
        //where catalog_id=".$this->catalogId;
        //$sqlStr = $sqlStr." order by create_time desc";
        $i = 0;
        $connection = Doctrine_Manager::connection();
        $STH = $connection->execute($sqlStr);
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        while ($row_array = $STH->fetch()) {
            $currentNews = array("photo_group_id" => $row_array["photo_group_id"], "title" => $row_array["title"], "currentTime" => $row_array["currentTime"], "snapshot" => $row_array["snapshot"]);
            $this->photoArray[] = $currentNews;
            $i++;
        }
    }

    public function executeDisplay(sfWebRequest $request)
    {
        if ($this->photoGroupId == null) {
            $this->photoGroupId = 1000;
        }
        if ($this->recordCount == null) {
            $this->recordCount = 9;
        }
        if ($this->homeuser_id == null) {
            $this->photoArray = array();
            $this->moreLinkDisplay = $request->getParameter("moreLinkDisplay", "none");
            $sqlStr = "select  photo_id,cast(brief as varchar(100)) brief, cast(link as varchar(100)) link,cast(order_i as varchar(100)) order_i from [Photo] where 1=1 and catalog_id>0 and photo_group_id=" . $this->photoGroupId;
            $connection = Doctrine_Manager::connection();
            $STH = $connection->execute($sqlStr);
            $STH->setFetchMode(PDO::FETCH_ASSOC);
            while ($row_array = $STH->fetch()) {
                $currentNews = array("photo_id" => $row_array["photo_id"], "brief" => $row_array["brief"], "link" => $row_array["link"], "order_i" => $row_array["order_i"]);
                $this->photoArray[] = $currentNews;
            }
        }
        else {
            $this->photoArray = array();
            $sqlStr = "select  photo_id,cast(brief as varchar(100)) brief, cast(link as varchar(100)) link,cast(order_i as varchar(100)) order_i from [Photo] where photo_group_id in(select photo_group_id from PhotoGroup where  1=1 and catalog_id>0 and user_id=" . $this->homeuser_id . ")";
            $connection = Doctrine_Manager::connection();
            $STH = $connection->execute($sqlStr);
            $STH->setFetchMode(PDO::FETCH_ASSOC);
            while ($row_array = $STH->fetch()) {
                $currentNews = array("photo_id" => $row_array["photo_id"], "brief" => $row_array["brief"], "link" => $row_array["link"], "order_i" => $row_array["order_i"]);
                $this->photoArray[] = $currentNews;
            }
        }
    }
}